const tagModel = require("../model/TagModel");
let {SUCCESS_RESPONSE} = require('../config/index')

exports.getTagsList = async (req, res) => {
    // 所有的查询字符串req.query接收的值都是字符串string类型
    let {
        page,
        pageSize,
        tag_name,
        is_show
    } = req.query

    // 1. 编写sql语句查询数据（model层）
    // 2. 查询数据
    let tags = await tagModel.getTagPage(
        page,
        pageSize,
        tag_name,
        is_show
    )

    console.log('1111  '+tag_name,'1111'+is_show)

    // // 查询符合条件的总记录数
    let count = await tagModel.getALlTagCount(
        tag_name,
        is_show
    )
    // 3. 返回
    res.json({
        code: '00000',
        data: {
            count, // 总记录数
            tags, // 分页数据
        },
        msg: '获取分类成功',
    })
}

exports.delTag = async (req, res) => {
    let {tag_id} = req.query
    console.log('111' + tag_id)
    let result = await tagModel.delTagById(tag_id)
    if (result.affectedRows > 0) {
        res.json({
            code: '00000',
            data: {},
            msg: 'Delete tag success',
        })
    } else {
        res.json({
            code: '00006',
            data: {},
            msg: 'Delete tag fail',
        })
    }
}

exports.delBatchTag = async (req, res) => {
    console.log(req.body)
    let {tag_ids} = req.body
    console.log('-----'+tag_ids)
    let result = await tagModel.delBatchTag(tag_ids)
    if (result.affectedRows > 0) {
        res.json({
            code: '00000',
            data: {},
            msg: 'Batch delete Tag success',
        })
    } else {
        res.json({
            code: '00007',
            data: {},
            msg: 'Batch Tag category fail',
        })
    }
}

exports.addTag = async (req, res) => {
    // 检测标签名是否重复
    let is_repeat = await tagModel.checkAddTagName(req.body.tag_name)
    if (is_repeat) {
        // 标签名不重复，添加标签
        let result = await tagModel.insertTag(req.body)
        if (result.affectedRows > 0) {
            res.json({
                code: '00000',
                data: {},
                msg: 'Add tag success',
            })
        } else {
            res.json({
                code: '00005',
                data: {},
                msg: 'Add tag fail',
            })
        }
    } else {
        res.json({
            code: '00008',
            data: {},
            msg: 'Add tag fail this tag already exists',
        })
    }
}

exports.updateTag = async (req, res) => {
    // 检测标签名是否重复
    let is_repeat = await tagModel.checkUpdateTagName(req.body.tag_id,req.body.tag_name)
    if (is_repeat) {
        // 标签名不重复，添加标签
        let result = await tagModel.updateTag(req.body)
        if (result.affectedRows > 0) {
            res.json({
                code: '00000',
                data: {},
                msg: 'Update tag success',
            })
        } else {
            res.json({
                code: '00005',
                data: {},
                msg: 'Update tag fail',
            })
        }
    } else {
        res.json({
            code: '00008',
            data: {},
            msg: 'Update tag fail this tag already exists',
        })
    }
}

exports.getAllTags = async (req, res) => {
    let tags =  await tagModel.getAllTags()
    res.json(SUCCESS_RESPONSE(tags))
}